package app.baseinfomanage.organmanage;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import app.base.common.Utility;

import app.baseinfomanage.organmanage.dao.OrganInfoDAO;
import app.baseinfomanage.organmanage.model.OrganInfo;

public class OrganLayerQuery extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException 
	{
	    request.setCharacterEncoding(Utility.CHARSET_ENCODING_UTF8);
	    response.setContentType(Utility.DEFAULT_CONTENT_TYPE);

		PrintWriter out = response.getWriter(); 
		
		String node = request.getParameter("node");
		String leaf = request.getParameter("leaf");
		String module = request.getParameter("module");	
		
	    if (!(node == null || node.trim().length() == 0))
	    {
		    node = URLDecoder.decode(node, Utility.CHARSET_ENCODING_UTF8).trim();
		    
			if (!(module == null))
			{
				module = URLDecoder.decode(module, Utility.CHARSET_ENCODING_UTF8).trim();
				node = node.substring(0, node.length() - module.length());
			}
	    	
			if (leaf == null || leaf.trim().length() == 0)
			{
				leaf = "2";
			}
			else
			{
				leaf = URLDecoder.decode(leaf, Utility.CHARSET_ENCODING_UTF8).trim();
			}
			
		    out.println(getTreeSubNodesByNodeId(node, leaf, module).toString());
	    }
		
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException 
	{
		doGet(request, response);
	}
	
	//return JSON format.
	@SuppressWarnings("unchecked")
	private StringBuffer getTreeSubNodesByNodeId(String nodeId, String leaf, String module)
	{
		StringBuffer json = new StringBuffer("[");
		
		ApplicationContext context = new ClassPathXmlApplicationContext(Utility.DEFAULT_SPRINGFRAMEWORK_APPLICATION_CONTEXT_FILE_PATH);
		OrganInfoDAO organInfoDAO = (OrganInfoDAO)context.getBean(Utility.ORGAN_MANAGE_MODULE_BEAN_NAME);
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("ORGAN_PARENT_ID", nodeId);
		map.put("LEAF_NODE_FLAG", leaf);
		
		List<OrganInfo> list = organInfoDAO.select_0001(map);
			
		for(int index = 0; index < list.size(); index++)
		{
			OrganInfo organInfo = (OrganInfo) list.get(index);
			
			String organId = (module == null) ? organInfo.getORGAN_ID().trim() : organInfo.getORGAN_ID().trim() + module;
			String organParentId = organInfo.getORGAN_PARENT_ID().trim();
			String organName = organInfo.getORGAN_NAME().trim();
			String organDesc = organInfo.getORGAN_DESC().trim();
			String normalIcon = organInfo.getNORMAL_IMG_URL().trim();
			String leafNodeFlag = organInfo.getLEAF_NODE_FLAG().trim();
			String expandIcon = organInfo.getEXPAND_IMG_URL() == null ? "" : organInfo.getEXPAND_IMG_URL().trim();
			String displayRankingIndex = organInfo.getDISPLAY_RANKING_INDEX().toString().trim();

			json.append("{id: '" + organId + "',");
			json.append("organParentId: '" + organParentId + "',");
			json.append("text: '" + organName + "',");
			json.append("qtip: '" + organDesc + "',");
			json.append(leafNodeFlag.equals("1") ? "leaf: true," : "leaf: false,");
			json.append("icon: '" + normalIcon + "',");
			json.append("expandIcon: '" + expandIcon + "',");
			json.append("displayRankingIndex: '" + displayRankingIndex + "',");
			json.append("singleClickExpand: true");

			json.append("}");
			
			if (index < list.size() - 1)
			{
				json.append(", ");
			}
		}
		
		json.append("]");
		
		return json;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}